package com.zhentao.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.schedule.file.article.pojo.ApArticleConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Date 2024/3/7 19:50
 * @Author xdm
 **/
@Component
public class ArticleDownOrUpListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "articleDownOrUpTopic")
    public void ArticleDownOrUp(String message) {
        System.out.println("获取到kafka的数据"+message);
        if (StringUtils.isNotBlank(message)) {
            Map map = JSON.parseObject(message, Map.class);
            //修改文章的状态
            QueryWrapper<ApArticleConfig> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleConfig::getArticleId,map.get("articleId"));
            ApArticleConfig config = apArticleConfigMapper.selectOne(queryWrapper);

            int getEnable = (int) map.get("enable");
            boolean enabl = true;
            if (getEnable == 1) {
                enabl = false;
            }
            config.setIsDown(enabl);
            apArticleConfigMapper.updateById(config);
        }
    }

}
